package com.orange.api.filter;

import org.eclipse.jetty.http.HttpStatus;
import server.IHandleSessionManager;
import server.rest.HttpContext;
import server.rest.ResponseException;
import server.rest.filter.AbstractRSFFilter;
import server.rest.filter.PrepareFilter;
import server.rest.sessions.SessionManagerController;
import annotation.Inject;
import com.orange.api.UserSession;

public class AuthFilter extends AbstractRSFFilter implements PrepareFilter
{

    @Inject(type = server.IHandleSessionManager.class)
    SessionManagerController<String, Long, UserSession> sessionManagerController;

    public String getName()
    {
        return "Auth";
    }

    public void doPreHandler(HttpContext context, Object... objects) throws ResponseException
    {
        String key = sessionManagerController.getKey(context);
        UserSession session = (UserSession) sessionManagerController.getSession(key);
        if (null == session)
        {
            throw new ResponseException(HttpStatus.FORBIDDEN_403, "The Session invalidation.");
        }
    }

    public void setSessionManagerController(IHandleSessionManager<String, Long, UserSession> provider)
    {
        if (null != provider)
        {
            this.sessionManagerController = provider.getSessionManager();
        }
    }

}
